package com.zhaojun.cloud.service.order.controller;

import com.zhaojun.cloud.common.exception.AssertionException;
import com.zhaojun.cloud.common.result.CommonResult;
import com.zhaojun.cloud.service.order.service.SeckillService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * wwj
 * 2019/1/2  16:52
 * <p>
 * 秒杀业务
 */
@RestController
@RequestMapping("/seckill")
public class SeckillController {


    private final Logger logger = LoggerFactory.getLogger(SeckillController.class);

    @Autowired
    private SeckillService seckillService;

    @RequestMapping("/dbSeckill")
    public CommonResult dbSeckill(@RequestParam String seckillId, @RequestParam String userId) {
        try {
            logger.info("用户" + userId + ":秒杀商品【" + seckillId + "】开始。。。。。。");
            seckillService.dbSeckill(seckillId, userId);
            return CommonResult.success("秒杀成功");
        } catch (AssertionException e1) {
            logger.error(e1.getMessage(), e1);
            return CommonResult.error(e1.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return CommonResult.error("秒杀失败");
        }
    }
}
